package com.abel.ignite.parser;

import com.abel.ignite.model.AtomType;
import com.abel.ignite.model.RepositoryModel;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xiachao
 * @date 2019-04-28
 */

public class DDLParser {

    public static String parseCreate(RepositoryModel model){
        if(model == null || StringUtils.isEmpty(model.getRepository())
            || CollectionUtils.isEmpty(model.getAtomTypes())){
            return null;
        }
        String table = model.getRepository();
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE ");
        sql.append(table);
        sql.append(" (id LONG PRIMARY KEY ");
        List<AtomType> atomTypes = model.getAtomTypes();
        for(AtomType atom : atomTypes){
            sql.append(", ");
            sql.append(atom.getName());
            sql.append(" ");
            sql.append(atom.getType());
        }
        sql.append(") WITH \"template=local\"");
        //template=replicated 复制模式
        //template=partitioned 分区模式
        return sql.toString();
    }


}
